package com.probiz.estore.sales.service.impl;

import java.util.Date;
import java.util.List;

import com.probiz.estore.common.model.sales.Coupon;
import com.probiz.estore.common.model.sales.PromoRule;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.sales.dao.PromoRuleDao;
import com.probiz.estore.sales.model.PromoRuleSearchCriteria;
import com.probiz.estore.sales.service.PromoRuleManager;


/**
 * Manager implementation for PromoRule, responsible for business processing, and communicate between web and persistence layer.
 */
public class PromoRuleManagerImpl extends GenericManagerImpl<PromoRule> implements PromoRuleManager {

	private PromoRuleDao promoRuleDao = null;

	/**
	 * @param promoRuleDao
	 *            the promoRuleDao to set
	 */
	public void setPromoRuleDao(PromoRuleDao promoRuleDao) {
		this.promoRuleDao = promoRuleDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = promoRuleDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(PromoRule entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(PromoRule entity) {

	}
	
	public List<PromoRule> getAllCartPromotionRulesInProcess(){
		return promoRuleDao.getAllCartPromotionRulesInProcess();
	}
	public List<PromoRule> getAllCatalogPromotionRulesInProcess(){
		return promoRuleDao.getAllCatalogPromotionRulesInProcess();
	}
	
	public List<PromoRule> getAllCouponPromotionRulesInProcess(){
		return promoRuleDao.getAllCouponPromotionRulesInProcess();
	}
	
	public PromoRule getCouponPromotionRule(Coupon _coupon){
		return promoRuleDao.getCouponPromotionRule(_coupon);
	}
	
	public List<PromoRule> getAllCouponTypesInProcessOrInFuture(){
		return promoRuleDao.getAllCouponTypesInProcessOrInFuture();
	}
	public List<PromoRule>  searchPromotionRules(SearchCriteria _searchCriteria,
			PromoRuleSearchCriteria _promoSearchCriteria){
		return promoRuleDao.searchPromotionRules(_searchCriteria, _promoSearchCriteria);
	}
	public List<PromoRule> getAllPromotionRulesInProcessForFront(){
		return promoRuleDao.getAllPromotionRulesInProcessForFront();
	}
	public List<PromoRule> getAllPromotionRulesInFutureForFront(){
		return promoRuleDao.getAllPromotionRulesInFutureForFront();
	}
	public void setState(PromoRule _rule) {
		PromoRule rule = _rule;
		Date now = new Date();
		int i = now.compareTo(rule.getStartTime());
		int j;
		if (rule.getEndTime() != null) {
			j = now.compareTo(rule.getEndTime());
		} else {
			j = -1;
		}
		
		if(rule.getStatus() == 1 ){
			if ((i > 0) && (j < 0)) {
				rule.setState(String.valueOf(PromoRule.STATE_DOING));
			} else if ((i < 0) && (j < 0)) {
				rule.setState(String.valueOf(PromoRule.STATE_FUTURE));
			} else {
				rule.setState(String.valueOf(PromoRule.STATE_PAST));
			}	
		}else{
			rule.setState(String.valueOf(PromoRule.STATE_INVALID));
		}
		return;
	}
	
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.service.PromoRuleManager#getPormoRuleIdByUri(java.lang.String)
	 */
	public Integer getPormoRuleIdByUri(String uri){
		String promoRuleId = uri.substring(uri.lastIndexOf("item"), uri.lastIndexOf("."));
        promoRuleId = promoRuleId.substring("item".length());
        return Integer.valueOf(promoRuleId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.service.PromoRuleManager#getUriByPromoRuleId(java.lang.Integer)
	 */
	public String getUriByPromoRuleId(Integer promoRuleId){
		StringBuffer url = new StringBuffer();
        url.append("/promo");
        url.append("/item");
        url.append(promoRuleId.toString());
        url.append(".html");
        return url.toString();
	}


}
